package com.eqdd.yiqidian.ui.gongneng.kehu;

import android.content.Intent;
import android.databinding.DataBindingUtil;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.ChooseLianxirenActivityCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LianxirenListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ZhangPageResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import org.reactivestreams.Publisher;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

public class ChooseLianxirenActivity extends BaseActivity {
    private ChooseLianxirenActivityCustom dataBinding;
    private SlimAdapterEx<LianxirenListResultBean> slimAdapterEx;
    private String cusid;
    private int pageNum;
    private ModelRx2DataSource<LianxirenListResultBean> dataSource;
    private MVCCoolHelper<List<LianxirenListResultBean>> mvcHelper;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_choose_lianxiren);
        initTopTitleBar(View.VISIBLE, "选择联系人");
    }
    @Override
    public void initData() {
        cusid = getIntent().getStringExtra("cusid");
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_03, new SlimInjector<LianxirenListResultBean>() {
            @Override
            public void onInject(LianxirenListResultBean data, IViewInjector injector) {
                injector.text(R.id.tv_upper_left_content, data.getName())
                        .text(R.id.tv_down_left_content, data.getDep()+" "+data.getPost())
                        .text(R.id.tv_right_center_content, data.getCellphone());
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    Intent intent = new Intent();
                    intent.putExtra("bean", slimAdapterEx.getDataItem(position));
                    setResult(Config.SUCCESS, intent);
                    finish();
                });
        dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                return OkGo.<ZhangPageResultBean<List<LianxirenListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.LIANXIREN_LIST)
                        .params("page", pageNum)
                        .params("getpeople", 0)
                        .params("owner", AppConstent.user.getGuid())
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("cusid", cusid)
                        .converter(new JsonConverter<ZhangPageResultBean<List<LianxirenListResultBean>>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<ZhangPageResultBean<List<LianxirenListResultBean>>>())
                        .flatMap(new Function<ZhangPageResultBean<List<LianxirenListResultBean>>, Publisher<List<LianxirenListResultBean>>>() {
                            @Override
                            public Publisher<List<LianxirenListResultBean>> apply(@NonNull ZhangPageResultBean<List<LianxirenListResultBean>> noticeWraperResultBeanHttpResult) throws Exception {
                                pageNum = page == 1 ? 0 : noticeWraperResultBeanHttpResult.getNextpage();
                                return Flowable.just(noticeWraperResultBeanHttpResult.getItems() == null ? new ArrayList() : noticeWraperResultBeanHttpResult.getItems());
                            }
                        });
            }
        }, 10);
        mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();

    }

    @Override
    public void setView() {
//        OkGo.<HttpResult<List<LianxirenListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.LIANXIREN_LIST)
//                .params("getpeople", 0)
//                .params("owner", GreenDaoUtil.getUser().getGuid())
//                .params("cusid", cusid)
//                .execute(new DialogCallBack<HttpResult<List<LianxirenListResultBean>>>(this) {
//                    @Override
//                    public void onSuccess(Response<HttpResult<List<LianxirenListResultBean>>> response) {
//                        HttpResult<List<LianxirenListResultBean>> httpResult = response.body();
//                        ToastUtil.showShort(httpResult.getMsg());
//                        if (httpResult.getStatus() == 200) {
//                            slimAdapterEx.updateData(httpResult.getItems());
//                        }
//                    }
//                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
